package com.nd.android.u.filestoragesystem.business;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.common.android.utils.httpRequest.AutoLoginHttpRequest;
import com.common.android.utils.httpRequest.IHttpRequest;
import com.nd.android.u.filestoragesystem.business.bean.FileInfo;
import com.nd.android.u.filestoragesystem.business.bean.FolderInfo;
import com.nd.android.u.filestoragesystem.business.bean.InterActionAttr;
import com.nd.android.u.filestoragesystem.business.bean.ParamKey;
import com.nd.android.u.filestoragesystem.business.com.MiddleRequestHeader;
import com.nd.android.u.filestoragesystem.externalInterface.FileUtilFactory;
import com.nd.android.u.filestoragesystem.externalInterface.IFile;
import com.nd.android.u.filestoragesystem.externalInterface.IFolder;
import com.nd.android.u.filestoragesystem.externalInterface.IInterActionAttr;
import com.product.android.business.ApplicationVariable;
import com.product.android.business.config.Configuration;
/**
 * 文件系统工具类
 * 
 * <br>Created 2014-7-1 下午4:53:42
 * @version  
 * @author   huangszh		
 *
 * @see
 */
public enum FileSystemUtil {

    INSTANCE;
    
    /**
     * 获取中间层系统的http请求
     * 
     * <br>Created 2014-6-12 上午9:57:41
     * @return IHttpRequest
     * @author        :  huangszh
     */
    public IHttpRequest getMiddleRequest() {        
        IHttpRequest request = AutoLoginHttpRequest.getInstance(
        		ApplicationVariable.INSTANCE.applicationContext,Configuration.OAP_APPID,1);
        request.setAgent(new MiddleRequestHeader());
        return request;
    }
    
    /**
     * 解析互动信息{ "_2221212" : {} ,"_21111" : {}}
     * 
     * <br>Created 2014-7-1 下午5:17:57
     * @param oid   文件id或者uid
     * @param interActionInfo   返回的互动信息 
     * @return  IInterActionAttr    返回null表示没有对应的信息
     * @author        :  huangszh
     */
    public IInterActionAttr parseInterActionAttr(long oid,JSONObject interActionInfo) {
        
        if(interActionInfo == null) {
            return null;
        }
		String key = "_" + oid;
        return parseInterActionAttr(interActionInfo.optJSONObject(key));
    }
    
    /**
     * 解析互动信息{"praise_num":1,"praise_done":0,....}
     * 
     * <br>Created 2014-7-2 下午4:56:10
     * @param interActionInfo    返回的互动信息
     * @return  IInterActionAttr
     * @author        :  huangszh
     */
    public IInterActionAttr parseInterActionAttr(JSONObject interActionInfo) {
        if(interActionInfo == null ) {
            return null;
        }
        IInterActionAttr attr = new InterActionAttr();
        attr.setPraises(interActionInfo.optInt("praise_num"));
        attr.setPraised(interActionInfo.optInt("praise_done"));
        attr.setFavorties(interActionInfo.optInt("favorite_num"));
        attr.setFavortied(interActionInfo.optInt("favorite_done"));
        attr.setReplys(interActionInfo.optInt("reply_num"));
        return attr;
    }
    
    /**
     * 将id列表转成JSONArray
     * 
     * <br>Created 2014-6-27 上午11:55:44
     * @param pids  图片id列表
     * @return  JSONArray
     * @author        :  huangszh
     */
    public JSONArray convertToJSONArray(ArrayList<Long> pids) {
        if(pids == null) {
            return null;
        }
        
        int length = pids.size();
        JSONArray array = new JSONArray();
        for(int i = 0; i < length ; i++) {
            array.put(pids.get(i));
        }
        return array;
    }
    
    /**
     * 拼接成中间层需要的url
     * 
     * <br>Created 2014-7-1 上午10:56:24
     * @param url   url中有中文的话需要encode
     * @return  String
     * @author        :  huangszh
     */
    public String getMiddleUrl(String url){
        StringBuilder result = new StringBuilder();
        result.append(FileUtilFactory.getInstance().getMiddleUrl());
        result.append("req?call_url=");
        result.append(url);
        return result.toString();
    }
    
    /**
     * 解析文件内容
     * 
     * <br>Created 2014-7-1 下午4:51:47
     * @param jObject   返回内容
     * @return  IFile
     * @author        :  huangszh
     */
    public IFile parseFileInfo(JSONObject jObject) {
        if(jObject == null) {
            return null;
        }
        IFile info = new FileInfo();
        info.setFid(jObject.optLong(ParamKey.FID));
        info.setFolderid(jObject.optLong(ParamKey.FOLDERID));
        info.setCloudid(jObject.optString(ParamKey.CLOUND_ID));
        info.setType(jObject.optInt(ParamKey.TYPE));
        info.setId(jObject.optLong(ParamKey.ID));
        info.setAppid(jObject.optInt(ParamKey.APP_ID));
        info.setPath(jObject.optString(ParamKey.PATH));
        info.setMd5(jObject.optString(ParamKey.MD5));
        info.setMd5(jObject.optString(ParamKey.MIME));
        info.setSort(jObject.optString(ParamKey.SORT));
        info.setExt(jObject.optString(ParamKey.EXT));
        info.setSize(jObject.optLong(ParamKey.SIZE));
        info.setWidth(jObject.optInt(ParamKey.WIDTH));
        info.setHeight(jObject.optInt(ParamKey.HEIGHT));
        info.setName(jObject.optString(ParamKey.NAME));
        info.setInfo(jObject.optJSONObject(ParamKey.INFO));
        info.setHits(jObject.optInt(ParamKey.HITS));
        info.setTime(jObject.optLong(ParamKey.TIME));
        info.setIp(jObject.optString(ParamKey.IP));
        info.setLocal(jObject.optString(ParamKey.LOCAL));
        info.setFlag(jObject.optInt(ParamKey.FLAG));
        return info;
    }

    /**
     * 解析文件夹信息
     * 
     * <br>Created 2014-7-1 下午4:51:53
     * @param jObject   返回内容
     * @return  IFolder
     * @author        :  huangszh
     */
    public IFolder parseFolderInfo(JSONObject jObject) {
        if(jObject == null) {
            return null;
        }
        IFolder info = new FolderInfo();
        info.setFolderId(jObject.optLong(ParamKey.FOLDERID));
        info.setUpFolderId(jObject.optLong(ParamKey.UP_ID));
        info.setPath(jObject.optString(ParamKey.PATH));
        info.setFolderName(jObject.optString(ParamKey.NAME));
        info.setFolderInfo(jObject.optJSONObject(ParamKey.INFO));
        info.setFolders(jObject.optInt(ParamKey.FOLDERS));
        info.setFiles(jObject.optInt(ParamKey.FILES));
        info.setLastFid(jObject.optLong(ParamKey.LAST_FID));
        info.setDefaultId(jObject.optLong(ParamKey.DEFAULT_FID));
        info.setCreateTime(jObject.optLong(ParamKey.TIME));
        info.setLocation(jObject.optString(ParamKey.LOCAL));
        
        return info;
    }
    
}
